import React, { useEffect } from 'react';
// import echarts from "echarts"

// import echarts from 'echarts/lib/echarts'// 引入 ECharts 主模块
import * as echarts from 'echarts';
import ReactEcharts from 'echarts-for-react';

export default function () {
  // 指定图表的配置项和数据
  var option = {
    title: {
      text: 'ECharts 入门示例',
    },
    tooltip: {},
    legend: {
      data: ['销量'],
    },
    xAxis: {
      data: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子'],
    },
    yAxis: {},
    series: [
      {
        name: '销量',
        type: 'bar',
        data: [5, 20, 36, 10, 10, 20],
      },
    ],
  };

  var myChart;

  useEffect(() => {
    // 基于准备好的dom，初始化echarts实例
    myChart = echarts.init(document.getElementById('main'));
    // 使用刚指定的配置项和数据显示图表。
    myChart.setOption(option);
  }, []);

  const getOption = () => {
    let option = {
      title: {
        text: 'ECharts 入门示例',
      },
      tooltip: {},
      legend: {
        data: ['销量'],
      },
      xAxis: {
        data: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子'],
      },
      yAxis: {},
      series: [
        {
          name: '销量',
          type: 'line',
          data: [5, 20, 36, 10, 10, 20],
        },
      ],
    };
    return option;
  };

  const getOptionBPie = () => {
    let option = {
      title: {
        text: 'text 标题',
        subtext: 'text 副标题',
        left: 'center',
      },
      tooltip: {
        trigger: 'item',
      },
      legend: {
        orient: 'vertical',
        left: 'left',
      },
      series: [
        {
          name: 'Access From',
          type: 'pie',
          radius: '50%',
          data: [
            { value: 1048, name: 'Search Engine' },
            { value: 735, name: 'Direct' },
            { value: 580, name: 'Email' },
            { value: 484, name: 'Union Ads' },
            { value: 300, name: 'Video Ads' },
          ],
          emphasis: {
            itemStyle: {
              shadowBlur: 10,
              shadowOffsetX: 0,
              shadowColor: 'rgba(0, 0, 0, 0.5)',
            },
          },
        },
      ],
    };
    return option;
  };
  return (
    <div style={{ background: '#fff', padding: '5px' }}>
      Index_01 第一个 echarts 的使用
      {/* 显示方式 一  */}
      <div
        id="main"
        style={{ width: '600px', height: '400px', border: '1px solid red' }}
      ></div>
      {/* 实现方式二 */}
      <ReactEcharts
        style={{ border: '1px solid #ccc', marginTop: '15px' }}
        option={getOption()}
      />
      <ReactEcharts
        style={{ border: '1px solid #ccc', marginTop: '15px' }}
        option={getOptionBPie()}
      />
    </div>
  );
}
